<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class CompanyModel extends CI_Model {

    public $table = 'companys';
    public $primary = 'company_id';

    function add($data) {
        $this->db->insert($this->table, $data);
    }

    function update($id, $data) {
        $this->db->where($this->primary, $id);
        return $this->db->update($this->table, $data);
    }

    /**
     * Frontend
     */
    public function getCompanys($limit, $start) {
        $query = $this->db->query("SELECT `company_id`, `name`, `avatar`, `businesses`, `user_id`, `slug`, `date`, `rate` FROM `companys` 
                            	     ORDER BY `company_id` DESC 
                            	      LIMIT {$start}, {$limit}");

        return $query->result_array();
    }
    
    /**
     * Frontend
     */
    public function getHighestRateCompanys($limit, $start) {
        $query = $this->db->query("SELECT `company_id`, `name`, `avatar`, `businesses`, `user_id`, `slug`, `date`, `rate` FROM `companys` 
                            	     ORDER BY `rate` DESC 
                            	      LIMIT {$start}, {$limit}");

        return $query->result_array();
    }
    
    /**
     * Frontend
     */
    public function getCompanysWithRecruitment($limit, $start) {
        $query = $this->db->query("SELECT `company_id`, `name`, `avatar`, `businesses`, `user_id`, `slug`, `date`, `rate` FROM `companys` 
                                    WHERE `date` != ''
                            	     ORDER BY `rate` DESC 
                            	      LIMIT {$start}, {$limit}");

        return $query->result_array();
    }

    /**
     * @view getCompanyBusiness() in frontend_helper.php
     */
    public function getCompanysByCategoryId($limit, $start, $categoryId) {
        // WHERE businesses IN (1,2,3,4);
        if ($categoryId == 210) {
            $categoryStr = 'IN (210,1,2,3,4,5,6,7,8,9,10,11,12,9910,9911,9912,9920,9921,9950,9951)';
        } else if ($categoryId == 211) {
            $categoryStr = 'IN (211,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,
  	                            99210,99211,99220,99221,99230,99231,99232,99240,99241,99250,99251,99252,99270,99271,99280.99281,99290,99291,
  	                            99300,99301,99340,99341,99342,99420,99421,99440,99441)';
        } else if ($categoryId == 212) {
            $categoryStr = 'IN (212,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64,65,66,67,68,69,70,71,72,73)';
        } else if ($categoryId == 213) {
            $categoryStr = 'IN (213,80,81,82,83,84,99810,99811,99812)';
        } else if ($categoryId == 214) {
            $categoryStr = 'IN (214,90,91,92)';
        } else if ($categoryId == 215) {
            $categoryStr = 'IN (215,100,101,102,103,104,105,106,107,108,109,110,111,112,113,114,115,991000,991001,991002,991010,991011)';
        } else if ($categoryId == 216) {
            $categoryStr = 'IN (216,120,121,122,123,124,125,126,127,128,991200,991201,991220,991221,991240,991241,991250,991251,991260,991261)';
        } else if ($categoryId == 217) {
            $categoryStr = 'IN (217,140,141,142,143,144,145,146,147,148,149)';
        } else if ($categoryId == 218) {
            $categoryStr = 'IN (218,160,161,162,163,164,165,166,167,168,169,170,171,172,173,174,175,176,177,178,179,180,
  	                            991610,991611,991620,991621,991630,991631,991632,991633)';
        } else if ($categoryId == 219) {
            $categoryStr = 'IN (219,190,191,192,193,194,195,196,197,198,199,
  	                            992190,992191,992192,991900,991901,991910,991911,991920,991921,991930,991931,991960,991961)';
        } else if ($categoryId == 300) {
            $categoryStr = 'IN (300,301,302,303,304,305,306,307,308,309,310,311,312,313,314,315,316,317,318,319,320,321,322,323,324,325,326,327,328,329,330,331,332,333,334,335,336,337,338,339,340,341)';
        } else if ($categoryId == 400) {
            $categoryStr = 'IN (400,401,402,403,404,405,406,407,408,409,410,411,412,413,414,415,416,417,418,419,420,421,422,423,424,425,426,427,428,429,430,431,432,433,434,435,436,437,438,439,440,441,442,443,444,445)';
        } else {
            $str = "$categoryId, 99{$categoryId}0,99{$categoryId}1,99{$categoryId}2,99{$categoryId}3";
            $categoryStr = "IN ($str)";
        }

        $query = $this->db->query("SELECT `company_id`, `name`, `avatar`, `businesses`, `user_id`, `slug`, `date`, `rate` FROM `companys`
									WHERE `businesses` {$categoryStr}
                            	     ORDER BY `company_id` DESC 
                            	      LIMIT {$start}, {$limit}");
        return $query->result_array();
    }

    public function updateViewed($slug) {
        return $this->db->query("UPDATE `companys` SET `viewed` = `viewed` + 1  
  	    								WHERE `slug` = '{$slug}'");
    }

    function getCompanyBySlug($slug) {
        $query = $this->db->query("SELECT * FROM  `companys`
  	                                 WHERE `slug` = '{$slug}'");
        return $query->row_array();
    }

    /**
     * Hiển thị chi tiết thông tin của công ty
     * 
     * @param $id Id của công ty
     * @return array Trả về thông tin chi tiết của một công ty
     */
    function getCompanyById($id) {
        $query = $this->db->query("SELECT * FROM  `companys`
  	                                 WHERE `company_id` = {$id}");
        return $query->row_array();
    }

    /**
     * Trả lại chi tiết thông tin của công ty cho việc edit
     * 
     * @param $user_id Id của người dùng đang đăng nhập được lưu trong session
     * @return array Trả về thông tin chi tiết của một công ty
     */
    function getCompanyByUserId($user_id) {
        $query = $this->db->query("SELECT * FROM  `companys`
  	                                 WHERE `user_id` = {$user_id}");
        return $query->row_array();
    }

    // Cập nhật giá trị rate cho doanh nghiệp
    function changeRate($company_id, $data) {
        $this->db->where($this->primary, $company_id);
    	return $this->db->update($this->table, $data);
    }

}